<template>
  <div class="edit-container">
    <el-form v-loading="formLoading" ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="处理人姓名：" prop="name">
        <el-input
            style="width: 500px"
            placeholder="请输入名称"
            maxlength="20"
            v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="处理文件：" prop="images">
        <el-button type="primary" @click="uploads.show=true" size="mini">选择文件</el-button>
      </el-form-item>
      <el-form-item  prop="images" style="display: flex" v-if="form.pic.length >= 1">
        <div v-for="item in form.pic" :key="item.index">
          <el-image v-if="form.pic.length >= 1"
                    style="width: 100px; height: 100px;margin-right: 20px;"
                    :src="item"
                    fit="fill">
          </el-image>
        </div>
      </el-form-item>
      <el-form-item  prop="images" style="display: flex" v-if="form.video.length > 0">
        <div v-for="item in form.video" :key="item.index">
          <video class="pic" :src="item" controls="true" autoplay="false" style="width: 100px; height: 100px;margin-right: 20px;"></video>
        </div>
      </el-form-item>
      <el-form-item label="处理内容：" prop="handleText">
        <Tinymce v-model="form.handleText"></Tinymce>
      </el-form-item>
      <el-form-item label="处理时间：" prop="handleTime">
        <el-date-picker
            v-model="form.handleTime"
            type="datetime"
            placeholder="选择日期"
            value-format="yyyy-MM-dd HH:mm:ss"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button v-loading="saveLoading" element-loading-background="rgba(0, 0, 0, 0.3)" type="primary" @click="handlerSubmit" v-has-permi="['admin:salvation:dispose']"
        :disabled="form.allocationStatus==3?true:false"
        >保存</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
    <el-dialog
        :visible.sync="uploads.show"
        title="上传文件"
        :append-to-body="true"
        destroy-on-close
        :close-on-click-modal="false"
        width="1000px">
      <UploadFile
          v-if="uploads.show"
          :multiple="uploads.multiple"
          @getFile="getFile"
          ref="uploads"
      />
    </el-dialog>
  </div>
</template>
<script>
import {detail, save1} from "@/api/salvation";
import {getForm} from "@/utils/utils";
import UploadFile from "@/components/File/UploadFile.vue";
import Tinymce from "@/components/Tinymce/index.vue";
export default {
  components: {Tinymce, UploadFile},
  props:{
    id:{
      default:null
    },
    statu:{
      default:null
    }
  },
  data(){
    return{
      form:{
        rescueId:null,
        id:null,
        name:'',
        handleText:'',
        handleTime:'',
        pic:[],
        video:[],
      },
      allocationStatus:null,
      rules:{
        name: [{ required: true, message: '请输入内容', trigger: ['blur'] }],
        handleText: [{ required: true, message: '请输入内容', trigger: ['blur'] }],
        handleTime: [{ required: true, message: '请选择时间', trigger: ['blur'] }],
      },
      roles:[],
      saveLoading:false,
      formLoading:false,
      uploads:{
        show:false,
        multiple:true,
      }
    }
  },
  mounted() {
    this.form.rescueId = this.id
    this.allocationStatus = this.statu
    if(this.id){
      // this.getDetail();
    }
  },
  methods:{
    getFile(fileList){
      fileList.filter((item)=>{
        if(item.substr(-3,3) == 'jpg' || item.substr(-3,3) == 'png'){
          this.form.pic.push(item)
        }else if(item.substr(-3,3) == 'mp4'){
          this.form.video.push(item)
        }
      })
      // this.form.pic = fileList
      this.uploads.show = false
    },
    getDetail(){
      this.formLoading=true
      detail(this.id).then(res=>{
        getForm(res,this.form);
        this.form = res
      }).finally(()=>{
        this.formLoading=false;
      })
    },
    handlerSubmit(){
      if(this.saveLoading||this.formLoading){
        return;
      }
      this.$refs.form.validate(valid=>{
        if(!valid)return;
        this.saveLoading=true;
        save1(this.form).then(()=>{
          this.$message.success('保存成功');
          this.close();
        }).finally(()=>{
          this.saveLoading=false
        })
      })
    },
    close(){
      this.$emit('hideEditDialog')
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep{
  .el-form-item__content{
    display: flex;
  }
}
</style>
